package com.loong.web.gp4j.view.compile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.web.gp4j.bean.param.messages.MessageParam;
import com.loong.web.gp4j.bean.param.messages.MessagePublishParam;
import com.loong.web.gp4j.bean.param.search.SearchParam;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.service.message.MessageService;
import com.loong.web.gp4j.service.user.UserService;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.intercept.auth.AuthType;

/**
 * 消息资源
 *
 * @author 张成轩
 */
@Controller
@RequestMapping("compile/messages")
public class MessagesViewMapping {

	@Resource
	private MessageService messageService;
	@Resource
	private UserService userService;

	/**
	 * 消息页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param page 分页
	 * @return 视图
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@AuthType(Auths.EDITOR)
	public ModelAndView messages(HttpServletRequest request, SearchParam param, PageParam page) {

		ModelAndView model = new ModelAndView("gp4j/compile/messages");
		model.addObject("messages", messageService.queryMessages(request, param, page));
		model.addObject("auths", userService.getAuths(request));
		model.addObject("order", messageService.getOrder());
		return model;
	}

	/**
	 * 新增消息页面
	 * 
	 * @param request 请求
	 * @return 视图
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	@AuthType(Auths.EDITOR)
	public ModelAndView add(HttpServletRequest request) {

		ModelAndView model = new ModelAndView("gp4j/compile/message-edit");
		return model;
	}

	/**
	 * 新增消息提交页面
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 视图
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> addSubject(HttpServletRequest request, @Valid MessageParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		return messageService.add(request, param);
	}

	/**
	 * 删除消息
	 * 
	 * @param request 请求
	 * @param id 消息id
	 * @return 返回对象
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> repwd(HttpServletRequest request, Integer id) {

		if (id == null)
			return ResultVoFactory.fail("消息id不能为空");
		return messageService.removeMessage(id);
	}

	/**
	 * 发布消息
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid j校验器
	 * @return 返回对象
	 */
	@RequestMapping(value = "publish", method = RequestMethod.POST, headers = "content-type=application/json")
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> publish(HttpServletRequest request, @RequestBody @Valid MessagePublishParam param,
			BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		return messageService.publishMessage(param);
	}
}
